home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / OutOfPhase1.1 Source / OutOfPhase Folder / CmdDlgTwoParams.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-05  |  13.0 KB  |  427 lines  |  [TEXT/KAHL]

  1. /* CmdDlgTwoParams.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "CmdDlgTwoParams.h"
  31. #include "Memory.h"
  32. #include "Screen.h"
  33. #include "EventLoop.h"
  34. #include "Menus.h"
  35. #include "TextEdit.h"
  36. #include "SimpleButton.h"
  37. #include "WrapTextBox.h"
  38. #include "DataMunging.h"
  39. #include "Main.h"
  40. #include "Alert.h"
  41. #include "Numbers.h"
  42.  
  43.  
  44. #define WINXSIZE (350)
  45.  
  46. #define PROMPTX (10)
  47. #define PROMPTY (5)
  48. #define PROMPTWIDTH (WINXSIZE - (2 * PROMPTX))
  49. #define PROMPTHEIGHT (50)
  50.  
  51. #define FIRSTBOXPROMPTX (PROMPTX)
  52. #define FIRSTBOXPROMPTY (PROMPTY + PROMPTHEIGHT + 5)
  53.  
  54. #define FIRSTBOXEDITWIDTH (140)
  55. #define FIRSTBOXEDITHEIGHT (21)
  56. #define FIRSTBOXEDITX (WINXSIZE - FIRSTBOXEDITWIDTH - 10)
  57. #define FIRSTBOXEDITY (FIRSTBOXPROMPTY - 3)
  58.  
  59. #define SECONDBOXPROMPTX (FIRSTBOXPROMPTX)
  60. #define SECONDBOXPROMPTY (FIRSTBOXPROMPTY + FIRSTBOXEDITHEIGHT + 3)
  61.  
  62. #define SECONDBOXEDITX (FIRSTBOXEDITX)
  63. #define SECONDBOXEDITY (SECONDBOXPROMPTY - 3)
  64. #define SECONDBOXEDITWIDTH (FIRSTBOXEDITWIDTH)
  65. #define SECONDBOXEDITHEIGHT (FIRSTBOXEDITHEIGHT)
  66.  
  67. #define CANCELBUTTONWIDTH (80)
  68. #define CANCELBUTTONHEIGHT (21)
  69. #define CANCELBUTTONX ((1 * WINXSIZE) / 4 - (CANCELBUTTONWIDTH / 2))
  70. #define CANCELBUTTONY (SECONDBOXEDITY + SECONDBOXEDITHEIGHT + 10)
  71.  
  72. #define OKBUTTONWIDTH (CANCELBUTTONWIDTH)
  73. #define OKBUTTONHEIGHT (CANCELBUTTONHEIGHT)
  74. #define OKBUTTONX ((3 * WINXSIZE) / 4 - (OKBUTTONWIDTH / 2))
  75. #define OKBUTTONY (CANCELBUTTONY)
  76.  
  77. #define WINYSIZE (CANCELBUTTONY + CANCELBUTTONHEIGHT + 20)
  78.  
  79.  
  80. typedef struct
  81.     {
  82.         WinType*                    ScreenID;
  83.         char*                            PromptText;
  84.         char*                            FirstPromptText;
  85.         TextEditRec*            FirstBoxEdit;
  86.         char*                            SecondPromptText;
  87.         TextEditRec*            SecondBoxEdit;
  88.         SimpleButtonRec*    OKButton;
  89.         SimpleButtonRec*    CancelButton;
  90.     } WindowRec;
  91.  
  92.  
  93. static void            RedrawWindow(WindowRec* Window)
  94.     {
  95.         CheckPtrExistence(Window);
  96.         TextEditFullRedraw(Window->FirstBoxEdit);
  97.         TextEditFullRedraw(Window->SecondBoxEdit);
  98.         RedrawSimpleButton(Window->OKButton);
  99.         RedrawSimpleButton(Window->CancelButton);
  100.         SetClipRect(Window->ScreenID,0,0,WINXSIZE,WINYSIZE);
  101.         DrawWrappedTextBox(Window->ScreenID,Window->PromptText,GetScreenFont(),9,
  102.             PROMPTX,PROMPTY,PROMPTWIDTH,PROMPTHEIGHT);
  103.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,Window->FirstPromptText,
  104.             StrLen(Window->FirstPromptText),FIRSTBOXPROMPTX,FIRSTBOXPROMPTY,ePlain);
  105.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,Window->SecondPromptText,
  106.             StrLen(Window->SecondPromptText),SECONDBOXPROMPTX,SECONDBOXPROMPTY,ePlain);
  107.     }
  108.  
  109.  
  110. /* present a dialog box that allows the user to edit two parameters */
  111. /* returns True if the user changes the value and clicks OK. */
  112. MyBoolean                CommandDialogTwoParams(char* Prompt, char* FirstBoxName,
  113.                                     double* FirstDataInOut, char* SecondBoxName, double* SecondDataInOut)
  114.     {
  115.         WindowRec*        Window;
  116.         char*                    StringTemp;
  117.         MyBoolean            LoopFlag;
  118.         MyBoolean            DoItFlag EXECUTE(= -31342);
  119.         MyBoolean            ReturnValue;
  120.         TextEditRec*    ActiveTextEdit;
  121.  
  122.         Window = (WindowRec*)AllocPtrCanFail(sizeof(WindowRec),
  123.             "CommandDialogTwoParams: WindowRec");
  124.         if (Window == NIL)
  125.             {
  126.              FailurePoint1:
  127.                 AlertHalt("There is not enough memory available to edit the "
  128.                     "command parameters.",NIL);
  129.                 return False;
  130.             }
  131.         Window->PromptText = Prompt;
  132.         Window->FirstPromptText = FirstBoxName;
  133.         Window->SecondPromptText = SecondBoxName;
  134.  
  135.         Window->ScreenID = MakeNewWindow(eModelessDialogWindow,eWindowNotClosable,
  136.             eWindowNotZoomable,eWindowNotResizable,DialogLeftEdge(WINXSIZE),
  137.             DialogTopEdge(WINYSIZE),WINXSIZE,WINYSIZE,(void (*)(void*))&RedrawWindow,Window);
  138.         if (Window->ScreenID == NIL)
  139.             {
  140.              FailurePoint2:
  141.                 ReleasePtr((char*)Window);
  142.                 goto FailurePoint1;
  143.             }
  144.         SetWindowName(Window->ScreenID,"Edit Command");
  145.  
  146.         Window->OKButton = NewSimpleButton(Window->ScreenID,"OK",OKBUTTONX,OKBUTTONY,
  147.             OKBUTTONWIDTH,OKBUTTONHEIGHT);
  148.         if (Window->OKButton == NIL)
  149.             {
  150.              FailurePoint3:
  151.                 KillWindow(Window->ScreenID);
  152.                 goto FailurePoint2;
  153.             }
  154.         SetDefaultButtonState(Window->OKButton,True);
  155.  
  156.         Window->CancelButton = NewSimpleButton(Window->ScreenID,"Cancel",CANCELBUTTONX,
  157.             CANCELBUTTONY,CANCELBUTTONWIDTH,CANCELBUTTONHEIGHT);
  158.         if (Window->CancelButton == NIL)
  159.             {
  160.              FailurePoint4:
  161.                 DisposeSimpleButton(Window->OKButton);
  162.                 goto FailurePoint3;
  163.             }
  164.  
  165.         Window->FirstBoxEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,GetScreenFont(),
  166.             9,FIRSTBOXEDITX,FIRSTBOXEDITY,FIRSTBOXEDITWIDTH,FIRSTBOXEDITHEIGHT);
  167.         if (Window->FirstBoxEdit == NIL)
  168.             {
  169.              FailurePoint5:
  170.                 DisposeSimpleButton(Window->CancelButton);
  171.                 goto FailurePoint4;
  172.             }
  173.         StringTemp = LongDoubleToString(*FirstDataInOut,12,1e-6,1e6);
  174.         if (StringTemp == NIL)
  175.             {
  176.              FailurePoint6:
  177.                 DisposeTextEdit(Window->FirstBoxEdit);
  178.                 goto FailurePoint5;
  179.             }
  180.         TextEditNewRawData(Window->FirstBoxEdit,StringTemp,SYSTEMLINEFEED);
  181.         ReleasePtr(StringTemp);
  182.         TextEditHasBeenSaved(Window->FirstBoxEdit);
  183.         TextEditDoMenuSelectAll(Window->FirstBoxEdit);
  184.  
  185.         Window->SecondBoxEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,GetScreenFont(),
  186.             9,SECONDBOXEDITX,SECONDBOXEDITY,SECONDBOXEDITWIDTH,SECONDBOXEDITHEIGHT);
  187.         if (Window->SecondBoxEdit == NIL)
  188.             {
  189.              FailurePoint7:
  190.                 goto FailurePoint6;
  191.             }
  192.         StringTemp = LongDoubleToString(*SecondDataInOut,12,1e-6,1e6);
  193.         if (StringTemp == NIL)
  194.             {
  195.              FailurePoint8:
  196.                 DisposeTextEdit(Window->SecondBoxEdit);
  197.                 goto FailurePoint7;
  198.             }
  199.         TextEditNewRawData(Window->SecondBoxEdit,StringTemp,SYSTEMLINEFEED);
  200.         ReleasePtr(StringTemp);
  201.         TextEditHasBeenSaved(Window->SecondBoxEdit);
  202.         TextEditDoMenuSelectAll(Window->SecondBoxEdit);
  203.  
  204.  
  205.         ActiveTextEdit = Window->FirstBoxEdit;
  206.         EnableTextEditSelection(ActiveTextEdit);
  207.         LoopFlag = True;
  208.         while (LoopFlag)
  209.             {
  210.                 OrdType                            X;
  211.                 OrdType                            Y;
  212.                 ModifierFlags                Modifiers;
  213.                 MenuItemType*                MenuItem;
  214.                 char                                KeyPress;
  215.  
  216.                 switch (GetAnEvent(&X,&Y,&Modifiers,NIL,&MenuItem,&KeyPress))
  217.                     {
  218.                         default:
  219.                             break;
  220.                         case eCheckCursor:
  221.                             if (TextEditIBeamTest(Window->FirstBoxEdit,X,Y)
  222.                                 || TextEditIBeamTest(Window->SecondBoxEdit,X,Y))
  223.                                 {
  224.                                     SetIBeamCursor();
  225.                                 }
  226.                              else
  227.                                 {
  228.                                     SetArrowCursor();
  229.                                 }
  230.                             goto UpdateCursorPoint;
  231.                             break;
  232.                         case eNoEvent:
  233.                          UpdateCursorPoint:
  234.                             TextEditUpdateCursor(ActiveTextEdit);
  235.                             break;
  236.                         case eMenuStarting:
  237.                             EnableMenuItem(mPaste);
  238.                             if (TextEditIsThereValidSelection(ActiveTextEdit))
  239.                                 {
  240.                                     EnableMenuItem(mCut);
  241.                                     EnableMenuItem(mCopy);
  242.                                     EnableMenuItem(mClear);
  243.                                 }
  244.                             EnableMenuItem(mSelectAll);
  245.                             if (TextEditCanWeUndo(ActiveTextEdit))
  246.                                 {
  247.                                     EnableMenuItem(mUndo);
  248.                                 }
  249.                             break;
  250.                         case eMenuCommand:
  251.                             if (MenuItem == mPaste)
  252.                                 {
  253.                                     TextEditDoMenuPaste(ActiveTextEdit);
  254.                                 }
  255.                             else if (MenuItem == mCut)
  256.                                 {
  257.                                     TextEditDoMenuCut(ActiveTextEdit);
  258.                                 }
  259.                             else if (MenuItem == mCopy)
  260.                                 {
  261.                                     TextEditDoMenuCopy(ActiveTextEdit);
  262.                                 }
  263.                             else if (MenuItem == mClear)
  264.                                 {
  265.                                     TextEditDoMenuClear(ActiveTextEdit);
  266.                                 }
  267.                             else if (MenuItem == mUndo)
  268.                                 {
  269.                                     TextEditDoMenuUndo(ActiveTextEdit);
  270.                                     TextEditShowSelection(ActiveTextEdit);
  271.                                 }
  272.                             else if (MenuItem == mSelectAll)
  273.                                 {
  274.                                     TextEditDoMenuSelectAll(ActiveTextEdit);
  275.                                 }
  276.                             else
  277.                                 {
  278.                                     EXECUTE(PRERR(AllowResume,
  279.                                         "CommandDialogTwoParams: Undefined menu option chosen"));
  280.                                 }
  281.                             break;
  282.                         case eKeyPressed:
  283.                             if (KeyPress == 13)
  284.                                 {
  285.                                     FlashButton(Window->OKButton);
  286.                                     DoItFlag = True;
  287.                                     LoopFlag = False;
  288.                                 }
  289.                             else if (KeyPress == 9)
  290.                                 {
  291.                                     DisableTextEditSelection(ActiveTextEdit);
  292.                                     if ((eShiftKey & Modifiers) == 0)
  293.                                         {
  294.                                             /* tab forwards */
  295.                                             if (ActiveTextEdit == Window->FirstBoxEdit)
  296.                                                 {
  297.                                                     ActiveTextEdit = Window->SecondBoxEdit;
  298.                                                 }
  299.                                             else
  300.                                                 {
  301.                                                     ActiveTextEdit = Window->FirstBoxEdit;
  302.                                                 }
  303.                                         }
  304.                                      else
  305.                                         {
  306.                                             /* tab backwards */
  307.                                             if (ActiveTextEdit == Window->SecondBoxEdit)
  308.                                                 {
  309.                                                     ActiveTextEdit = Window->FirstBoxEdit;
  310.                                                 }
  311.                                             else
  312.                                                 {
  313.                                                     ActiveTextEdit = Window->SecondBoxEdit;
  314.                                                 }
  315.                                         }
  316.                                     TextEditDoMenuSelectAll(ActiveTextEdit);
  317.                                     EnableTextEditSelection(ActiveTextEdit);
  318.                                 }
  319.                             else if (KeyPress == eCancelKey)
  320.                                 {
  321.                                     FlashButton(Window->CancelButton);
  322.                                     DoItFlag = False;
  323.                                     LoopFlag = False;
  324.                                 }
  325.                             else
  326.                                 {
  327.                                     TextEditDoKeyPressed(ActiveTextEdit,KeyPress,Modifiers);
  328.                                 }
  329.                             break;
  330.                         case eMouseDown:
  331.                             if (SimpleButtonHitTest(Window->OKButton,X,Y))
  332.                                 {
  333.                                     if (SimpleButtonMouseDown(Window->OKButton,X,Y,NIL,NIL))
  334.                                         {
  335.                                             DoItFlag = True;
  336.                                             LoopFlag = False;
  337.                                         }
  338.                                 }
  339.                             else if (SimpleButtonHitTest(Window->CancelButton,X,Y))
  340.                                 {
  341.                                     if (SimpleButtonMouseDown(Window->CancelButton,X,Y,NIL,NIL))
  342.                                         {
  343.                                             DoItFlag = False;
  344.                                             LoopFlag = False;
  345.                                         }
  346.                                 }
  347.                             else if (TextEditHitTest(Window->FirstBoxEdit,X,Y))
  348.                                 {
  349.                                     if (ActiveTextEdit != Window->FirstBoxEdit)
  350.                                         {
  351.                                             DisableTextEditSelection(ActiveTextEdit);
  352.                                             ActiveTextEdit = Window->FirstBoxEdit;
  353.                                             EnableTextEditSelection(ActiveTextEdit);
  354.                                         }
  355.                                     TextEditDoMouseDown(ActiveTextEdit,X,Y,Modifiers);
  356.                                 }
  357.                             else if (TextEditHitTest(Window->SecondBoxEdit,X,Y))
  358.                                 {
  359.                                     if (ActiveTextEdit != Window->SecondBoxEdit)
  360.                                         {
  361.                                             DisableTextEditSelection(ActiveTextEdit);
  362.                                             ActiveTextEdit = Window->SecondBoxEdit;
  363.                                             EnableTextEditSelection(ActiveTextEdit);
  364.                                         }
  365.                                     TextEditDoMouseDown(ActiveTextEdit,X,Y,Modifiers);
  366.                                 }
  367.                             break;
  368.                     }
  369.             }
  370.         ERROR((DoItFlag != True) && (DoItFlag != False),PRERR(ForceAbort,
  371.             "CommandDialogTwoParams:  DoItFlag is neither true nor false"));
  372.  
  373.         ReturnValue = False;
  374.  
  375.         if (DoItFlag)
  376.             {
  377.                 MyBoolean                    ErrorNoMemory;
  378.  
  379.                 ErrorNoMemory = False;
  380.  
  381.                 if (TextEditDoesItNeedToBeSaved(Window->FirstBoxEdit))
  382.                     {
  383.                         StringTemp = TextEditGetRawData(Window->FirstBoxEdit,SYSTEMLINEFEED);
  384.                         if (StringTemp == NIL)
  385.                             {
  386.                                 ErrorNoMemory = True;
  387.                             }
  388.                          else
  389.                             {
  390.                                 *FirstDataInOut = StringToLongDouble(StringTemp,PtrSize(StringTemp));
  391.                                 ReleasePtr(StringTemp);
  392.                                 ReturnValue = True;
  393.                             }
  394.                     }
  395.  
  396.                 if (TextEditDoesItNeedToBeSaved(Window->SecondBoxEdit))
  397.                     {
  398.                         StringTemp = TextEditGetRawData(Window->SecondBoxEdit,SYSTEMLINEFEED);
  399.                         if (StringTemp == NIL)
  400.                             {
  401.                                 ErrorNoMemory = True;
  402.                             }
  403.                          else
  404.                             {
  405.                                 *SecondDataInOut = StringToLongDouble(StringTemp,PtrSize(StringTemp));
  406.                                 ReleasePtr(StringTemp);
  407.                                 ReturnValue = True;
  408.                             }
  409.                     }
  410.  
  411.                 if (ErrorNoMemory)
  412.                     {
  413.                         AlertHalt("There was not enough memory available to save all of "
  414.                             "the attributes.",NIL);
  415.                     }
  416.             }
  417.  
  418.         DisposeSimpleButton(Window->OKButton);
  419.         DisposeSimpleButton(Window->CancelButton);
  420.         DisposeTextEdit(Window->FirstBoxEdit);
  421.         DisposeTextEdit(Window->SecondBoxEdit);
  422.         KillWindow(Window->ScreenID);
  423.         ReleasePtr((char*)Window);
  424.  
  425.         return ReturnValue;
  426.     }
  427.